package com.jsyh.maidu.activity;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.support.annotation.CheckResult;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.ViewStub;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.AbsListView;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.jsyh.maidu.activity.me.CreateOrderActivity;
import com.jsyh.maidu.activity.me.LoginActivity;
import com.jsyh.maidu.activity.me.StoreActivity;
import com.jsyh.maidu.adapter.CommentListAdapter;
import com.jsyh.maidu.adapter.PopUpAdapter;
import com.jsyh.maidu.config.ConfigValue;
import com.jsyh.maidu.config.SPConfig;
import com.jsyh.maidu.model.BaseModel;
import com.jsyh.maidu.model.CommentModel;
import com.jsyh.maidu.model.GoodsAttr;
import com.jsyh.maidu.model.GoodsBuyModel;
import com.jsyh.maidu.model.GoodsInfoModel2;
import com.jsyh.maidu.model.Rank;
import com.jsyh.maidu.model.ShopInfoModel;
import com.jsyh.maidu.presenter.CollectionPresenter;
import com.jsyh.maidu.presenter.CommentListPreserter;
import com.jsyh.maidu.presenter.DetailsPresenter;
import com.jsyh.maidu.presenter.GoodsBuyPresenter;
import com.jsyh.maidu.utils.AppManager;
import com.jsyh.maidu.utils.MenuPopupWindow;
import com.jsyh.maidu.utils.ParamPopupWindow;
import com.jsyh.maidu.utils.SPUtils;
import com.jsyh.maidu.utils.Utils;
import com.jsyh.maidu.views.CommentView;
import com.jsyh.maidu.views.GoodDetailsView;
import com.jsyh.maidu.views.GoodsBuyView;
import com.jsyh.maidu.views.ShopInfoView;
import com.jsyh.shopping.uilibrary.R;
import com.jsyh.shopping.uilibrary.bannerview.BannerView;
import com.squareup.picasso.Picasso;

import java.util.ArrayList;
import java.util.List;

import static com.jsyh.maidu.utils.TimeUtils.setTime;

public class GoodsInfoActivity extends BaseActivity implements GoodDetailsView, OnClickListener,
        CompoundButton.OnCheckedChangeListener, GoodsBuyView, ShopInfoView, CommentView, ParamPopupWindow.OnSubmitListener {

    private ListView list_baseContent;

    private RelativeLayout rlContentView;
    private CheckBox mCheckBoxCollection;
    private Button btn_addCart;   //加入购物车
    private Button btn_buy;   //立即购买

    private ImageButton right_imbt;
    private ImageButton left_imbt;
    private BannerView head_View;
    private GoodsBuyPresenter goodsBuyPresenter;

    private DetailsPresenter mPresenter;

    private GoodsInfoModel2 mDatas;             //商品详情数据

    private TextView mTextViewGoodsName, mTextViewGoodsPrice, mTextViewGoodsAttr, tv_sale, goodsBrief;
    private TextView priceType, restTime, marketPrice, goodsPoint, goodsDiscount, comment, vipLevel;
    private TextView goodsParam, shopOwner, shopName, shopTotal, shopStar, shopScore, shopTransfer;
    private ImageView goodsShopImage;
    private Button collect, enter;

    private LinearLayout shopLayout, shopInfoLayout, shopActionLayout, paramLayout;

    private CollectionPresenter collectionPresenter;

    //网络错误用到的view
    private ViewStub mNetworStub;
    private View mNetworkErrorLayout;
    private Button mReloadRequest;

    View foot_View;

    private PopupWindow popInfo;
    RecyclerView popContent;
    Button submit;
    TextView popTitle;
    PopUpAdapter adapter;
    List<String> key = new ArrayList<>();
    List<String> value = new ArrayList<>();
    View rootView;

    View goodsIndicator, detailIndicator, reviewIndicator;
    TextView goodsTv, detailTv, reviewTv;

    WebView goodsDetail;
    String content;
    RecyclerView commentList;
    LinearLayoutManager layoutManager;
    CommentListAdapter commentAdapter;
    CommentListPreserter commentpareserter;
    List<CommentModel.CommentInfo> comments;
    ParamPopupWindow window;

    private String attr_value_id = "";
    private String attr_id = "";
    private String num = "1";

    private static final int PARAM = 1;
    private static final int ADD_CART = 2;
    private static final int BUY = 3;
    private int current;

    private ImageButton goCart;
    private ImageButton menuImg;
    private MenuPopupWindow menu;

    @Override
    public void initView() {
        super.initView();
        setContentView(R.layout.activity_goodsinfo);
        rootView = LayoutInflater.from(this).inflate(R.layout.activity_goodsinfo, null);
        AppManager.getAppManager().addActivity(this);
        mNetworStub = (ViewStub) findViewById(com.jsyh.maidu.R.id.vsNetworkError);
        mNetworkErrorLayout = mNetworStub.inflate();
        mReloadRequest = (Button) mNetworkErrorLayout.findViewById(com.jsyh.maidu.R.id.btnReloadNetwork);
        mReloadRequest.setOnClickListener(this);
        mNetworkErrorLayout.setVisibility(View.GONE);

        rlContentView = (RelativeLayout) findViewById(R.id.rlContentView);
        //头部
        head_View = new BannerView(this);
        head_View.setLayoutParams(new AbsListView.LayoutParams(AbsListView.LayoutParams.MATCH_PARENT, Utils.dip2px(this, 300)));
        head_View.setDrawerMode(head_View.STYLE_Dot);
        head_View.setLocationMode(head_View.MODE_CENTER);
        list_baseContent = (ListView) findViewById(R.id.list_baseContent);
        //内容部分
        foot_View = LayoutInflater.from(mContext).inflate(R.layout.goodinfo_foot_view, null);
        mTextViewGoodsName = (TextView) foot_View.findViewById(R.id.goodsinfo_name);
        mTextViewGoodsPrice = (TextView) foot_View.findViewById(R.id.goodsinfo_price);
        mTextViewGoodsAttr = (TextView) foot_View.findViewById(R.id.goodsinfo_attr);
        tv_sale = (TextView) foot_View.findViewById(R.id.tv_goodinfo_sale);

        goodsBrief = (TextView) foot_View.findViewById(R.id.goods_brief);
        priceType = (TextView) foot_View.findViewById(R.id.price_type);
        restTime = (TextView) foot_View.findViewById(R.id.rest_time);
        marketPrice = (TextView) foot_View.findViewById(R.id.goods_market_price);
        goodsPoint = (TextView) foot_View.findViewById(R.id.goods_score);
        goodsDiscount = (TextView) foot_View.findViewById(R.id.goods_sale);
        comment = (TextView) foot_View.findViewById(R.id.goods_comment);
        goodsParam = (TextView) foot_View.findViewById(R.id.goods_param);
        goodsShopImage = (ImageView) foot_View.findViewById(R.id.goods_shop_img);
        shopName = (TextView) foot_View.findViewById(R.id.goods_shop_name);
        shopTotal = (TextView) foot_View.findViewById(R.id.shop_goods_total);
        shopStar = (TextView) foot_View.findViewById(R.id.shop_starer_total);
        shopScore = (TextView) foot_View.findViewById(R.id.shop_score_goods);
        shopOwner = (TextView) foot_View.findViewById(R.id.shop_score_owner);
        shopTransfer = (TextView) foot_View.findViewById(R.id.shop_score_transfer);
        goodsParam.setOnClickListener(this);

        shopLayout = (LinearLayout) foot_View.findViewById(R.id.shop_layout);
        shopInfoLayout = (LinearLayout) foot_View.findViewById(R.id.shop_info_layout);
        shopActionLayout = (LinearLayout) foot_View.findViewById(R.id.shop_action_layout);
        shopInfoLayout.setOnClickListener(this);

        vipLevel = (TextView) foot_View.findViewById(R.id.goods_level_info);
        vipLevel.setOnClickListener(this);

        collect = (Button) foot_View.findViewById(R.id.collect_shop);
        enter = (Button) foot_View.findViewById(R.id.enter_shop);
        paramLayout = (LinearLayout) foot_View.findViewById(R.id.param_layout);
        collect.setOnClickListener(this);
        enter.setOnClickListener(this);

        findViewById(R.id.goods).setOnClickListener(this);
        findViewById(R.id.goods_detail).setOnClickListener(this);
        findViewById(R.id.goods_review).setOnClickListener(this);
        goodsIndicator = findViewById(R.id.goods_view);
        detailIndicator = findViewById(R.id.goods_detail_view);
        reviewIndicator = findViewById(R.id.goods_review_view);
        goodsTv = (TextView) findViewById(R.id.goods_tv);
        detailTv = (TextView) findViewById(R.id.goods_detail_tv);
        reviewTv = (TextView) findViewById(R.id.goods_review_tv);
        commentList = (RecyclerView) findViewById(R.id.goods_comment_list);
        layoutManager = new LinearLayoutManager(this);
        commentList.setLayoutManager(layoutManager);


        goodsDetail = (WebView) findViewById(R.id.goods_detail_web);
        goodsDetail.setWebViewClient(new WebViewClient() {
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                return false;
            }
        });
        goodsDetail.getSettings().setJavaScriptEnabled(true);

        mTextViewGoodsAttr.setOnClickListener(this);

        list_baseContent.addHeaderView(head_View);
        list_baseContent.addFooterView(foot_View);

        list_baseContent.setAdapter(null);

        View view = LayoutInflater.from(this).inflate(R.layout.goods_info_pop, null);
        popInfo = new PopupWindow(view, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
        popContent = (RecyclerView) view.findViewById(R.id.pop_content);
        submit = (Button) view.findViewById(R.id.pop_submit);
        popContent.setLayoutManager(new LinearLayoutManager(this));
        submit.setOnClickListener(this);
        popTitle = (TextView) view.findViewById(R.id.pop_title);
        //
        btn_addCart = (Button) findViewById(R.id.btn_addCart);
        btn_buy = (Button) findViewById(R.id.btn_buy);
        mCheckBoxCollection = (CheckBox) findViewById(R.id.txt_collection);
        goCart = (ImageButton) findViewById(R.id.detail_goto_cart);
        menuImg = (ImageButton) findViewById(R.id.right_imbt);
        menuImg.setOnClickListener(this);
        goCart.setOnClickListener(this);

        //加入购物车
        btn_addCart.setOnClickListener(this);
        //立即购买
        btn_buy.setOnClickListener(this);

        View title = findViewById(R.id.detail_title);

        right_imbt = (ImageButton) title.findViewById(R.id.right_imbt);

        left_imbt = (ImageButton) title.findViewById(R.id.left_imbt);
        left_imbt.setImageDrawable(getResources().getDrawable(R.mipmap.ic_back));
        left_imbt.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

    }

    private void initPop(String title, List<String> key, List<String> value) {
        popTitle.setText(title);
        adapter = new PopUpAdapter(key, value, this);
        popContent.setAdapter(adapter);
    }

    @Override
    public void initData() {
        super.initData();
        mPresenter = new DetailsPresenter(mContext);
        if (Utils.NO_NETWORK_STATE != Utils.isNetworkAvailable(this)) {
            mPresenter.LoadDetailsData(getGoodsId());
        }
    }

    @Override
    public void albumData(String[] albums) {
        for (int i = 0; i < albums.length; i++) {
            albums[i] = ConfigValue.IMG_IP + albums[i];
        }
        head_View.initAdapterData(albums);

    }

    @Override
    public void onLoadGoodsInfoDatas(@Nullable GoodsInfoModel2 datas) {
        if (datas != null) {
            GoodsInfoModel2.GoodsInfo info = datas.getData();
            List<String> albums = info.getGoods_detail().getAlbum();
            String[] album = albums.toArray(new String[albums.size()]);
            mDatas = datas;
            albumData(album);
            content = info.getGoods_detail().getContent();
            //属性界面商品缩略图

            mTextViewGoodsName.setText(info.getGoods_detail().getGoods_name());
            mTextViewGoodsPrice.setText("￥" + info.getGoods_detail().getTotal_price());
            initInfo();
            if (!TextUtils.isEmpty(info.getSupplier_id())) {
                if (Integer.valueOf(info.getSupplier_id()) > 0) {
                    collectionPresenter = new CollectionPresenter(this);
                    collectionPresenter.getShopInfo(this, info.getSupplier_id());
                }
            }
            if (info.getAttribute().size() > 0) {
                paramLayout.setVisibility(View.VISIBLE);
                mTextViewGoodsPrice.setText("￥" + info.getGoods_detail().getTotal_price());
                mTextViewGoodsAttr.setText("产品属性：" + info.getAttribute_title());
                StringBuilder sb = new StringBuilder();
                StringBuilder sb2 = new StringBuilder();
                for (GoodsInfoModel2.Attribute attr : info.getAttribute()) {
                    for (GoodsInfoModel2.Attribute.Attr_Value value : attr.getAttr_value()) {
                        if (value.getDefaults().equals("1")) {
                            sb.append(value.getAttr_value_id()).append(",");
                            sb2.append(attr.getAttr_id()).append("-").append(value.getAttr_value_id()).append(",");
                        }
                    }
                }
                attr_value_id = sb.substring(0, sb.length() - 1).toString();
                attr_id = sb2.substring(0, sb2.length() - 1).toString();
            }
            if (info.getGoods_detail().getSales() == null) {
                tv_sale.setText("0人已购");
            } else {
                tv_sale.setText(info.getGoods_detail().getSales() + "人已购");
            }
            //判断是否为已经关注
            if (Integer.valueOf(info.getGoods_detail().getIs_attention()) == 1)
                mCheckBoxCollection.setChecked(true);

            //关注
            mCheckBoxCollection.setOnCheckedChangeListener(this);
        }
    }

    private void initInfo() {
        if (!TextUtils.isEmpty(mDatas.getData().getGoods_detail().getGoods_brief())) {
            goodsBrief.setText(mDatas.getData().getGoods_detail().getGoods_brief());
            goodsBrief.setVisibility(View.VISIBLE);
        }
        marketPrice.setText("价格：" + mDatas.getData().getGoods_detail().getMarket_price());
        if (!TextUtils.isEmpty(mDatas.getData().getGoods_detail().getGive_integral())) {
            goodsPoint.setText("赠送积分：" + mDatas.getData().getGoods_detail().getGive_integral());
            goodsPoint.setVisibility(View.VISIBLE);
        }
        if (!TextUtils.isEmpty(mDatas.getData().getGoods_detail().getCoupon_daoji())) {
            if (Integer.valueOf(mDatas.getData().getGoods_detail().getCoupon_daoji()) > 0) {
                String time = mDatas.getData().getGoods_detail().getCoupon_daoji();
                restTime.setVisibility(View.VISIBLE);
                priceType.setVisibility(View.VISIBLE);
                long timeInt = Long.valueOf(time) * 1000;
                new CountDownTimer(timeInt, 1000) {

                    @Override
                    public void onTick(long millisUntilFinished) {
                        restTime.setText("剩余 " + setTime(millisUntilFinished / 1000));
                    }

                    @Override
                    public void onFinish() {

                    }
                }.start();
            }

        }
        goodsDiscount.setText("折扣：" + mDatas.getData().getGoods_detail().getZhekou() + "折");
        comment.setText(mDatas.getData().getGoods_detail().getComment_num() + "人评价");
    }

    @Override
    public void onAddCarShopping(@Nullable BaseModel data) {
        Utils.showToast(this, data.getMsg());
        if (data != null && data.getCode().equals(ConfigValue.Success_Code)) {
            //加入成功
            //1. 修改 购物车图片 圆点增加
            int goodsNums = Integer.valueOf(num);
            Intent mIntent = new Intent(ConfigValue.ACTION_ALTER_CARTGOODS_NUMS);
            mIntent.putExtra("addGoodsNums", goodsNums);
            //发送广播
            sendBroadcast(mIntent);
        }

    }

    //收藏回调
    @Override
    public void onCollectGoods(BaseModel data) {
        Utils.showToast(this, data.getMsg());
        if (data.getCode().equals("1")) {
            mCheckBoxCollection.setChecked(true);
        } else if (data.getCode().equals("-220")) {

            Intent itLogin = new Intent(this, LoginActivity.class);
            startActivity(itLogin);
        } else
            mCheckBoxCollection.setChecked(false);
    }

    //取消商品回调
    @Override
    public void cancelCollectGoods(BaseModel data) {
        Utils.showToast(this, data.getMsg());
        if (data.getCode().equals("1")) {
            mCheckBoxCollection.setChecked(false);
        } else if (data.getCode().equals("-220")) {

            Intent itLogin = new Intent(this, LoginActivity.class);
            startActivity(itLogin);
        } else
            mCheckBoxCollection.setChecked(true);
    }

    /**
     * 获取传递的 商品 ID
     *
     * @return
     */
    @CheckResult
    private String getGoodsId() {
        Bundle extras = getIntent().getExtras();
        if (extras == null) {
            throw new IllegalArgumentException("必须传递数据");
        }
        String goodsId = extras.getString("goodsId");
        if (TextUtils.isEmpty(goodsId)) {
            throw new NullPointerException();
        }
        return goodsId;

    }

    private void initParamPop() {
        if (window == null) {
            window = new ParamPopupWindow(this, mDatas.getData().getAttribute(),
                    mDatas.getData().getGoods_detail().getGoods_id(), attr_value_id, num, this, attr_id);
            window.show(rootView);
        } else {
            window.show(rootView);
        }

    }

    @Override
    public void onClick(View v) {
        if (Utils.NO_NETWORK_STATE == Utils.isNetworkAvailable(this)) {
            switchLayout();
            Utils.showToast(this, getResources().getString(R.string.network_errot_toast));
            return;
        } else {
            mNetworkErrorLayout.setVisibility(View.GONE);

            rlContentView.setVisibility(View.VISIBLE);

            int goodsNums = 0;
            switch (v.getId()) {
                case R.id.goodsinfo_attr:
                    initParamPop();
                    current = PARAM;
                    break;
                case R.id.btn_addCart:
                    initParamPop();
                    current = ADD_CART;
                    break;
                case R.id.btn_buy:

                    if (!SPUtils.get(this, "key", "").equals("")) {
                        initParamPop();
                        current = BUY;


                    } else {
                        Intent itLogin = new Intent(this, LoginActivity.class);
                        startActivity(itLogin);
                    }
                    break;

                case R.id.btnReloadNetwork:
                    mPresenter.LoadDetailsData(getGoodsId());
                    onResume();
                    break;

                case R.id.collect_shop:
                    if (TextUtils.isEmpty((String) SPUtils.get(this, SPConfig.KEY, ""))) {
                        Intent intent = new Intent(this, LoginActivity.class);
                        startActivity(intent);
                        return;
                    }
                    collectionPresenter.collectShop(this, mDatas.getData().getSupplier_id());
                    break;
                case R.id.enter_shop:
                    Intent intent = new Intent(GoodsInfoActivity.this, StoreActivity.class);
                    intent.putExtra("suppid", mDatas.getData().getSupplier_id());
                    startActivity(intent);
                    break;
                case R.id.goods_level_info:
                    key.clear();
                    value.clear();
                    for (Rank rank : mDatas.getData().getRank_price()) {
                        key.add(rank.getRank_name());
                        value.add(rank.getRank_price());
                    }
                    initPop("会员专享价", key, value);
                    popInfo.setAnimationStyle(-1);
                    popInfo.showAtLocation(rootView, Gravity.BOTTOM, 0, 0);
                    break;

                case R.id.pop_submit:
                    if (popInfo.isShowing()) {
                        popInfo.dismiss();
                    }
                    break;

                case R.id.goods_param:
                    key.clear();
                    value.clear();
                    GoodsInfoModel2.Base base = mDatas.getData().getParams().getBase();
                    if (!TextUtils.isEmpty(base.getBrand_name())) {
                        key.add("品牌名称");
                        value.add(base.getBrand_name());
                    }
                    if (!TextUtils.isEmpty(base.getGoods_name())) {
                        key.add("商品名称");
                        value.add(base.getGoods_name());
                    }
                    if (!TextUtils.isEmpty(base.getGoods_sn())) {
                        key.add("商品编号");
                        value.add(base.getGoods_sn());
                    }
                    if (!TextUtils.isEmpty(base.getAdd_time())) {
                        key.add("上架时间");
                        value.add(base.getAdd_time());
                    }
                    if (!TextUtils.isEmpty(base.getGoods_weight())) {
                        key.add("商品重量");
                        value.add(base.getGoods_weight());
                    }
                    if (!TextUtils.isEmpty(base.getGoods_number())) {
                        key.add("商品库存");
                        value.add(base.getGoods_number());
                    }
                    for (GoodsInfoModel2.Other other : mDatas.getData().getParams().getOther()) {
                        key.add(other.getAttr_name());
                        value.add(other.getAttr_value());
                    }
                    initPop("商品信息", key, value);
                    popInfo.setAnimationStyle(-1);
                    popInfo.showAtLocation(rootView, Gravity.BOTTOM, 0, 0);
                    break;

                case R.id.shop_info_layout:
                    Intent itent = new Intent(GoodsInfoActivity.this, StoreActivity.class);
                    itent.putExtra("suppid", mDatas.getData().getSupplier_id());
                    startActivity(itent);
                    break;

                case R.id.goods:
                    setIndicator(0);
                    list_baseContent.setVisibility(View.VISIBLE);
                    goodsDetail.setVisibility(View.GONE);
                    commentList.setVisibility(View.GONE);
                    break;
                case R.id.goods_detail:
                    setIndicator(1);
                    goodsDetail.setVisibility(View.VISIBLE);
                    list_baseContent.setVisibility(View.GONE);
                    String newHtml = content.replaceAll("<img", "<img width=\"100%\"");
                    goodsDetail.loadDataWithBaseURL(ConfigValue.IMG_IP, newHtml, "text/html", "utf-8", null);
                    list_baseContent.setVisibility(View.GONE);
                    commentList.setVisibility(View.GONE);
                    break;
                case R.id.goods_review:
                    setIndicator(2);
                    commentpareserter = new CommentListPreserter(this, this, mDatas.getData().getGoods_detail().getGoods_id(), "0");
                    commentpareserter.LoadComment();
                    commentList.setVisibility(View.VISIBLE);
                    goodsDetail.setVisibility(View.GONE);
                    list_baseContent.setVisibility(View.GONE);
                    break;

                case R.id.detail_goto_cart:
                    Intent carintent = new Intent(this, ShoppingCartActivity.class);
                    startActivity(carintent);
                    break;

                case R.id.right_imbt:
                    if (menu == null) {
                        menu = new MenuPopupWindow(this);
                        menu.show(menuImg);
                    } else {
                        if (menu.isShowing()) {
                            menu.hide();
                        } else {
                            menu.show(menuImg);
                        }
                    }
                    break;
            }
        }
    }

    private void setIndicator(int index) {
        switch (index) {
            case 0:
                goodsTv.setTextColor(Color.parseColor("#dd2726"));
                goodsIndicator.setVisibility(View.VISIBLE);
                detailTv.setTextColor(Color.parseColor("#666666"));
                detailIndicator.setVisibility(View.INVISIBLE);
                reviewTv.setTextColor(Color.parseColor("#666666"));
                reviewIndicator.setVisibility(View.INVISIBLE);
                break;
            case 1:
                goodsTv.setTextColor(Color.parseColor("#666666"));
                goodsIndicator.setVisibility(View.INVISIBLE);
                detailTv.setTextColor(Color.parseColor("#dd2726"));
                detailIndicator.setVisibility(View.VISIBLE);
                reviewTv.setTextColor(Color.parseColor("#666666"));
                reviewIndicator.setVisibility(View.INVISIBLE);
                break;

            case 2:
                goodsTv.setTextColor(Color.parseColor("#666666"));
                goodsIndicator.setVisibility(View.INVISIBLE);
                detailTv.setTextColor(Color.parseColor("#666666"));
                detailIndicator.setVisibility(View.INVISIBLE);
                reviewTv.setTextColor(Color.parseColor("#dd2726"));
                reviewIndicator.setVisibility(View.VISIBLE);
                break;
        }

    }

    @Override
    public void onBackPressed() {
        if (popInfo.isShowing()) {
            popInfo.dismiss();
            return;
        }
        super.onBackPressed();

    }

    @Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        if (Utils.NO_NETWORK_STATE != Utils.isNetworkAvailable(this)) {
            if (!SPUtils.get(this, "key", "").equals("")) {
                if (isChecked)
                    mPresenter.addCollect(getGoodsId());
                else
                    mPresenter.cancelCollect(getGoodsId());
            } else {
                if (isChecked)
                    mCheckBoxCollection.setChecked(false);
                else
                    mCheckBoxCollection.setChecked(true);
                Intent itLogin = new Intent(this, LoginActivity.class);
                startActivity(itLogin);
            }
        } else {
            if (isChecked)
                mCheckBoxCollection.setChecked(false);
            else
                mCheckBoxCollection.setChecked(true);
            switchLayout();
        }
    }

    //此处加载购物车商品数量

    @Override
    protected void onResume() {
        super.onResume();
        if (Utils.NO_NETWORK_STATE == Utils.isNetworkAvailable(this)) {
            switchLayout();
        } else {

        }
    }

    public void switchLayout() {
        mNetworkErrorLayout.setVisibility(View.VISIBLE);
        rlContentView.setVisibility(View.GONE);
    }

    @Override
    public void getGoodsCar(GoodsBuyModel model) {
        if (!model.getCode().equals("1")) {

            Toast.makeText(this, model.getMsg(), Toast.LENGTH_LONG).show();
        } else {
            Intent intent = new Intent(this, CreateOrderActivity.class);

            intent.putExtra("intentType", "1");
            intent.putExtra("Carid", model.getData().getRec_id());
            startActivity(intent);
        }

    }

    @Override
    public void getShopInfo(ShopInfoModel model) {
        if (model.getCode().equals("1")) {
            Picasso.with(this).load(ConfigValue.IMG_IP + model.getData().getSupplierinfo().getLogo()).into(goodsShopImage);
            shopName.setText(model.getData().getSupplierinfo().getSupplier_name() + "\n\n" + model.getData().getSupplierinfo().getRank_name());
            shopTotal.setText(model.getData().getSupplierinfo().getNumber() + "\n全部商品");
            shopStar.setText(model.getData().getSupplierinfo().getFans() + "\n关注人数");
            shopScore.setText("宝贝描述" + model.getData().getSupplierinfo().getComment_rank());
            shopOwner.setText("卖家服务" + model.getData().getSupplierinfo().getServer());
            shopTransfer.setText("物流服务" + model.getData().getSupplierinfo().getShipping());
            shopLayout.setVisibility(View.VISIBLE);
            shopInfoLayout.setVisibility(View.VISIBLE);
            shopActionLayout.setVisibility(View.VISIBLE);
        }
    }

    @Override
    public void getCollectShopResult(BaseModel model) {
        Toast.makeText(this, model.getMsg(), Toast.LENGTH_SHORT).show();
    }

    @Override
    public void ReCommentList(CommentModel comment) {
        comments = new ArrayList<>();
        comments = comment.getData();
        commentAdapter = new CommentListAdapter(this, comments);
        commentList.setAdapter(commentAdapter);
    }

    @Override
    public void submit(GoodsAttr attr) {
        attr_id = attr.getAttr_id();
        attr_value_id = attr.getAttr_value_id();
        num = attr.getNum();
        switch (current) {
            case PARAM:
                mTextViewGoodsAttr.setText(attr.getChecked());
                break;
            case ADD_CART:
                boolean isAdd = mPresenter.addShoppingCar(num, mDatas.getData().getGoods_detail().getGoods_id(), attr_id);
                if (!isAdd) {
                    Toast.makeText(this, "添加失败", Toast.LENGTH_SHORT).show();
                }
                break;
            case BUY:
                if (Integer.valueOf(num) > 0) {
                    goodsBuyPresenter = new GoodsBuyPresenter(this, this, mDatas.getData().getGoods_detail().getGoods_id(), num, attr_id);
                    goodsBuyPresenter.loadGoodscar();

                } else Utils.showToast(this, "请填写商品数量！");
                break;
        }
    }
}

